<template>
  <view class="UserInfoDetail">
    <!-- 讨论列表 -->
    <view class="discussionList" v-if="title === '我的讨论'">
      <MyDiscussion
        :discussionDetail="item"
        v-for="(item, index) in userDiscussion"
        :key="index"
        operator="删除"
        :operatorWidth="80"
      />
    </view>

    <!-- 收藏列表(讨论) -->
    <view class="collectionList" v-if="title === '我的收藏'">
      <MyDiscussion
        :discussionDetail="item"
        v-for="(item, index) in collectionDiscussion"
        :key="index"
        operator="取消收藏"
        :operatorWidth="100"
      />
    </view>

    <!-- 队伍列表 -->
    <view class="teamList" v-if="title === '我的队伍'">
      <MyTeam
        v-for="(item, index) in userTeam"
        :key="index"
        :teamDetail="item"
        @deleteTeam="handleDeleteTeam"
        @changeTeamInfo="handleOpenTeamInfo"
        @refreshTeamList="handleRefreshTeamList"
        @openKickDialog="handleKickDialog"
      />
    </view>

    <!-- 任务列表 -->
    <view class="taskList" v-if="title === '我的任务'">
      <MyTaskList
        v-for="(item, index) in userTaskList"
        :key="index"
        :taskListDetail="item"
        @refreshTaskList="handleRefreshTaskList"
      />
    </view>

    <!-- 关注列表 -->
    <view class="focusList" v-if="title === '我的关注'">
      <MyFocus
        v-for="(item, index) in userFocus"
        :key="index"
        :focusDetail="item"
        @refreshUserFocus="handleRefreshUserFocus"
      />
    </view>

    <!-- 队伍对话框 -->
    <TeamDialog
      v-if="showUpdateDialog"
      :teamDetail="changeTeamInfo"
      @closeDialog="showUpdateDialog = false"
      @saveTeamInfo="handleSaveTeamInfo"
    />
    <KickDialog
      v-if="showKickDialog"
      :initPosition="kickPosition"
      :teamInfo="teamInfo"
      @closeKickDialog="handleCloseKickDialog"
    />

    <!-- 空内容组件 -->
    <EmptyBox v-if="showEmpty" />
  </view>
</template>

<script>
import MyDiscussion from "./components/MyDiscussion.vue";
import MyTeam from "./components/MyTeam.vue";
import MyFocus from "./components/MyFocus.vue";
import MyTaskList from "./components/MyTaskList.vue";
import TeamDialog from "./components/TeamDialog.vue";
import KickDialog from "./components/KickDialog.vue";
import EmptyBox from "../../components/public/EmptyBox.vue";
import { mapState } from "vuex";
export default {
  components: {
    MyDiscussion,
    EmptyBox,
    MyTeam,
    MyFocus,
    MyTaskList,
    TeamDialog,
    KickDialog,
  },
  onLoad(options) {
    const { title } = options;
    this.title = title;
    this.initGetData(title);
    this.initEmpty(title);
    wx.setNavigationBarTitle({
      title,
    });
  },
  data() {
    return {
      title: "",
      showEmpty: false,
      showUpdateDialog: false,
      showKickDialog: false,
      changeTeamInfo: {},
      // 踢人对话框使用
      teamInfo: {},
      kickPosition: {},
    };
  },
  computed: {
    ...mapState([
      "userInfo",
      "userDiscussion",
      "collectionDiscussion",
      "userTeam",
      "joinTeam",
      "userFocus",
      "userTaskList",
    ]),
  },
  beforeDestroy() {
    this.$store.commit("clearUserDiscussion");
    this.$store.commit("clearUserTeam");
    this.$store.commit("clearUserTaskList");
    this.$store.commit("clearUserFocus");
  },

  methods: {
    // 初始化管理获取数据
    initGetData(title) {
      if (title === "我的讨论") {
        this.getUserDiscussionList();
      } else if (title === "我的收藏") {
        this.getUserCollectionList();
      } else if (title === "我的队伍") {
        this.getUserTeamList();
      } else if (title === "我的关注") {
        this.getUserFocusList();
      } else if (title === "我的任务") {
        this.getUserTaskList();
      }
    },

    // 获取我的讨论列表数据
    async getUserDiscussionList() {
      const discussionIdList = this.userInfo.discussion,
        type = "myDiscussion";
      if (this.userDiscussion.length !== discussionIdList.length) {
        for (let i = 0; i < discussionIdList.length; i++) {
          const discussionId = discussionIdList[i];
          await this.$store.dispatch("getOneUserDiscussion", {
            discussionId,
            type,
          });
        }
      }
    },

    // 获取我的收藏讨论列表数据
    async getUserCollectionList() {
      const discussionIdList = this.userInfo.collection,
        type = "myCollection";
      if (this.collectionDiscussion.length !== discussionIdList.length) {
        for (let i = 0; i < discussionIdList.length; i++) {
          const discussionId = discussionIdList[i];
          await this.$store.dispatch("getOneUserDiscussion", {
            discussionId,
            type,
          });
        }
      }
    },

    // 获取我的队伍列表数据
    async getUserTeamList() {
      const teamList = this.userInfo.myTeam,
        type = "myTeam";
      if (this.userTeam.length !== teamList.length) {
        for (let i = 0; i < teamList.length; i++) {
          const teamId = teamList[i];
          await this.$store.dispatch("getOneUserTeam", { teamId, type });
        }
      }
    },

    // 获取我的关注列表
    async getUserFocusList() {
      const focusList = this.userInfo.focusList,
        type = 3;
      if (this.userFocus.length !== focusList.length) {
        for (let i = 0; i < focusList.length; i++) {
          const userId = focusList[i];
          await this.$store.dispatch("getUserInfo", { userId, type });
        }
      }
    },
    // 获取我的任务列表
    async getUserTaskList() {
      const list1 = this.userInfo.teamTaskList,
        list2 = this.userInfo.taskList,
        taskList = [...list1, ...list2];
      console.log("查看长度：", this.userTaskList.length, taskList.length);
      if (this.userTaskList.length !== taskList.length) {
        for (let i = 0; i < taskList.length; i++) {
          const taskId = taskList[i];
          await this.$store.dispatch("getOneUserTaskList", taskId);
        }
      }
    },

    // 初始化空数据展示
    initEmpty(title) {
      if (title === "我的讨论" && !this.userInfo.discussion.length) {
        this.showEmpty = true;
        return;
      }
      if (title === "我的收藏" && !this.userInfo.collection.length) {
        this.showEmpty = true;
        return;
      }
      if (title === "我的队伍" && !this.userInfo.myTeam.length) {
        this.showEmpty = true;
        return;
      }
      if (title === "我的关注" && !this.userInfo.focusList.length) {
        this.showEmpty = true;
        return;
      }
      if (
        title === "我的任务" &&
        !this.userInfo.taskList.length &&
        !this.userInfo.teamTaskList.length
      ) {
        this.showEmpty = true;
        return;
      }
    },

    // 保存队伍信息
    async handleSaveTeamInfo(item) {
      const teamInfo = {},
        teamId = item._id;
      if (item.title.length) {
        teamInfo.title = item.title;
      }
      if (item.introduce.length) {
        teamInfo.introduce = item.introduce;
      }
      if (item.password.length) {
        teamInfo.password = item.password;
      }
      if (item.tags.length) {
        teamInfo.tags = item.tags;
      }
      uni.showLoading({
        title: "保存中",
      });
      try {
        await this.$store.dispatch("m_team/changeTeamInfo", {
          teamInfo,
          teamId,
        });
        this.$store.commit("clearUserTeam");
        await this.getUserTeamList();
        uni.hideLoading();
        uni.showToast({
          icon: "success",
          title: "修改成功",
        });
        this.showUpdateDialog = false;
      } catch (err) {
        uni.hideLoading();
        uni.showToast({
          icon: "error",
          title: "修改失败",
        });
      }
    },

    // 展示踢人对话框
    handleKickDialog(info) {
      this.teamInfo = info.teamInfo;
      this.kickPosition = info.position;
      this.showKickDialog = true;
    },

    // 关闭踢人对话框，判断是否需要更新队伍列表
    async handleCloseKickDialog(refresh) {
      this.showKickDialog = false;
      if (refresh) {
        this.$store.commit("clearUserTeam");
        await this.getUserTeamList();
      }
    },

    // 刷新队伍列表
    handleRefreshTeamList() {
      this.$store.commit("clearUserTeam");
      this.getUserTeamList();
    },

    // 刷新关注列表
    handleRefreshUserFocus() {
      this.$store.commit("clearUserFocus");
      this.getUserFocusList();
    },

    // 刷新任务列表
    handleRefreshTaskList() {
      console.log("checkrefresh");
      this.$store.commit("clearUserTaskList");
      this.getUserTaskList();
    },

    // 打开队伍信息dialog
    handleOpenTeamInfo(item) {
      this.changeTeamInfo = item;
      this.showUpdateDialog = true;
    },
  },
};
</script>

<style lang="scss" scoped>
.UserInfoDetail {
  position: relative;
  // padding: 20rpx;
}
</style>
